/**
 * 
 */
package edu.fjnu.fundsys.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.fjnu.fundsys.dao.AccountTransinfoDao;
import edu.fjnu.fundsys.domain.AccountTransinfo;
import edu.fjnu.fundsys.domain.Client;
import edu.fjnu.fundsys.utils.DBUtils;

/**
 * @author BaiXiang
 *
 */
public class AccountTransinfoDaoJDBCImpl implements AccountTransinfoDao {

	private static final String ADD_ACCOUNT_TRANSINFO = "insert into financial_account_transinfo values(seq_account_trans_id.nextval,?,?,?,to_date('2012-12-31 15:45:10','YYYY-MM-DD HH24:MI:SS'),'0000')";
	private static final String LOAD_ALL = "select * from financial_account_transinfo order by trans_id desc";
	private static final String LOAD_BYNO = "select * from financial_account_transinfo where account_no = ? order by trans_id desc";
	private static final String CNT_INFOS = "select count(*) cnt_infos from financial_account_transinfo where account_no = ?";
	
	public void add(AccountTransinfo accounttransinfo) {
		// TODO Auto-generated method stub
		
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		
		try {
			pstmt = conn.prepareStatement(ADD_ACCOUNT_TRANSINFO);
			pstmt.setString(1, accounttransinfo.getTransType());
			pstmt.setDouble(2, accounttransinfo.getTransAmount());
			pstmt.setInt(3, accounttransinfo.getAccountNo());
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, null);
		}
	}

	public List<AccountTransinfo> loadAll() {
		// TODO Auto-generated method stub
		
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<AccountTransinfo> accountTransinfoList = null;
		
		try{
			pstmt = conn.prepareStatement(LOAD_ALL);
			rset = pstmt.executeQuery();
			accountTransinfoList = new ArrayList<AccountTransinfo>();
			while(rset.next()){
				AccountTransinfo accountTransinfo = new AccountTransinfo();
				
				accountTransinfo.setTransId(rset.getInt("trans_id"));
				accountTransinfo.setTransType(rset.getString("trans_type"));
				accountTransinfo.setTransAmount(rset.getDouble("trans_amount"));
				accountTransinfo.setAccountNo(rset.getInt("account_no"));
				accountTransinfo.setTransTime(rset.getString("trans_time"));
				accountTransinfo.setOperCode(rset.getString("oper_code"));
				
				
				accountTransinfoList.add(accountTransinfo);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return accountTransinfoList;
	}

	public List<AccountTransinfo> loadByNo(Integer accountNo) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<AccountTransinfo> accountTransinfoList = null;
		
		try{
			pstmt = conn.prepareStatement(LOAD_BYNO);
			pstmt.setInt(1, accountNo);
			rset = pstmt.executeQuery();
			accountTransinfoList = new ArrayList<AccountTransinfo>();
			while(rset.next()){
				AccountTransinfo accountTransinfo = new AccountTransinfo();
				
				accountTransinfo.setTransId(rset.getInt("trans_id"));
				accountTransinfo.setTransType(rset.getString("trans_type"));
				accountTransinfo.setTransAmount(rset.getDouble("trans_amount"));
				accountTransinfo.setAccountNo(rset.getInt("account_no"));
				accountTransinfo.setTransTime(rset.getString("trans_time"));
				accountTransinfo.setOperCode(rset.getString("oper_code"));
				
				
				accountTransinfoList.add(accountTransinfo);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return accountTransinfoList;
	}

	public int cntInfos(Integer accountNo) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		int cnt = 0;
		
		try{
			pstmt = conn.prepareStatement(CNT_INFOS);
			pstmt.setInt(1, accountNo);
			rset = pstmt.executeQuery();

			if(rset.next()){
				cnt = rset.getInt("cnt_infos");
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return cnt;
	}

	public List<AccountTransinfo> getSelectInfos(Integer accountNo,Integer begin,Integer end) {
		// TODO Auto-generated method stub
		
		String sql = "select * from (select rownum rn, a.* from (select * from financial_account_transinfo where account_no ="+accountNo+" order by trans_id desc) a where rownum<=? ) where rn>=?";
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<AccountTransinfo> accountTransinfoList = null;
		
		try{
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, end);
			pstmt.setInt(2, begin);
			rset = pstmt.executeQuery();
			accountTransinfoList = new ArrayList<AccountTransinfo>();
			while(rset.next()){
				AccountTransinfo accountTransinfo = new AccountTransinfo();
				
				accountTransinfo.setTransId(rset.getInt("trans_id"));
				accountTransinfo.setTransType(rset.getString("trans_type"));
				accountTransinfo.setTransAmount(rset.getDouble("trans_amount"));
				accountTransinfo.setAccountNo(rset.getInt("account_no"));
				accountTransinfo.setTransTime(rset.getString("trans_time"));
				accountTransinfo.setOperCode(rset.getString("oper_code"));
				
				
				accountTransinfoList.add(accountTransinfo);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return accountTransinfoList;
	}


	
}
